# $Id: libutils.py 1.1 2004/12/30 03:56:21 jimb Exp $
# Python utility library.
# Author:   Jim Brooks  http://www.jimbrooks.org
# Date:     2004/08
# License:  GNU General Public License (GPL)
# Requires: Python 2.3, PyGame, SDL.
# ==============================================================================

# Prune invalid objects in a list.
# Requires: Objects in elements have a .valid member.
#           List is flat (no nested sequences).
# Iterating thru a list and deleting items
# is an error in Python so a copy is used.
def PruneList( list ):
    tmp = list[:]     # copy list (shallow copy)
    i = -1
    for obj in tmp:
        i += 1
        if not obj.valid:
            del list[i]
            i -= 1
    return

# Prune invalid objects in a list of lists (2D list).
# Requires: Objects in elements have a .valid member.
#           List must not be deeper than 2 levels.
def PruneListList( listList ):
    tmpListList = listList[:] # copy list (shallow copy)
    i = -1
    for list2 in tmpListList: # for each 2nd-level list
        i += 1
        j = -1
        for obj in list2:
            j += 1
            if not obj.valid:
                del listList[i][j]
                j -= 1
    return
